<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\common\service;


use app\api\logic\ChapterLogic;
use app\api\logic\PromotionLinkLogic;
use app\common\logic\BaseLogic;
use app\common\model\book\Book;
use app\common\model\book\Bookread;
use app\common\model\Config;
use app\common\model\promotion\PromotionLink;
use app\common\model\tags\UserTags;
use app\common\model\user\User as UserModel;
use app\common\model\buy\EgoldCountLog as EgoldCountLogModel;
use app\common\model\buy\EgoldLog as EgoldLogModel;
use app\common\model\buy\BookAutobuy as BookAutobuyModel;
use app\lib\MessageBus\base\AmqpBus;
use app\lib\MessageBus\BusEnum;


class TagsService extends BaseLogic
{

    /**
     * 用户标签相关
     *
     * @param $user
     * @param int $type 行为类型 1充值 2签到 3支付未阅读
     * @return bool
     */
    public static function matchTags(int $type,$user,$order=null)
    {
        $userId = $user['id'] ?? 0;
        if (!$userId) return false;
        //用户充值：增加延时队列，5分钟如果还没阅读，打充值未阅读标签
        switch ($type) {
            case 1:
                //判断用户是否有充值标签
                $userTags = UserTags::where(['user_id'=>$userId,'tag_id'=>1])->findOrEmpty();
                if ($userTags->isEmpty()){
                    UserTags::create([
                        'user_id'=>$userId,
                        'tag_id'=>1,
                        'platform'=>$user['platform'],
                    ]);
                }
                //判断是否为活动订单或者年费订单
                if (!empty($order)){
                    //判断订单是否为活动订单
                    if ($order->type==7){
                        $userTags = UserTags::where(['user_id'=>$userId,'tag_id'=>4])->findOrEmpty();
                        if ($userTags->isEmpty()){
                            UserTags::create([
                                'user_id'=>$userId,
                                'tag_id'=>4,
                                'platform'=>$user['platform'],
                            ]);
                        }
                    }
                    //判断订单是否为年费订单
                    if ($order->type==6){
                        $userTags = UserTags::where(['user_id'=>$userId,'tag_id'=>8])->findOrEmpty();
                        if ($userTags->isEmpty()){
                            UserTags::create([
                                'user_id'=>$userId,
                                'tag_id'=>8,
                                'platform'=>$user['platform'],
                            ]);
                        }
                    }
                }
                $signInTags = UserTags::where(['user_id'=>$userId,'tag_id'=>2])->findOrEmpty();
                if (!$signInTags->isEmpty()){
                    UserTags::where(['user_id'=>$userId,'tag_id'=>2])->delete();
                }
                //小额用户，20以下
                if ($user->paymoney<20){
                    $userTags = UserTags::where(['user_id'=>$userId,'tag_id'=>5])->findOrEmpty();
                    if ($userTags->isEmpty()){
                        UserTags::create([
                            'user_id'=>$userId,
                            'tag_id'=>5,
                            'platform'=>$user['platform'],
                        ]);
                    }
                }
                if ($user->paymoney>=20&&$user->paymoney<=40){
                    $userTags = UserTags::where(['user_id'=>$userId,'tag_id'=>6])->findOrEmpty();
                    if ($userTags->isEmpty()){
                        UserTags::create([
                            'user_id'=>$userId,
                            'tag_id'=>6,
                            'platform'=>$user['platform'],
                        ]);
                    }
                    UserTags::where(['user_id'=>$userId])->where('tag_id','=',5)->delete();
                }
                if ($user->paymoney>40){
                    $userTags = UserTags::where(['user_id'=>$userId,'tag_id'=>7])->findOrEmpty();
                    if ($userTags->isEmpty()){
                        UserTags::create([
                            'user_id'=>$userId,
                            'tag_id'=>7,
                            'platform'=>$user['platform'],
                        ]);
                    }
                    UserTags::where(['user_id'=>$userId])->where('tag_id','in',[5,6])->delete();
                }
                AmqpBus::instance()->publish(
                    [
                        "user_id"  => $user->id,
                        "user"     => $user,
                        "order"     => $order,
                        "bus_name"   => BusEnum::PAYMENT_TAGS,
                    ]
                );
                break;
            case 2:
                //签到未充值
                if ($user->paynum==0){
                    $userTags = UserTags::where(['user_id'=>$userId,'tag_id'=>2])->findOrEmpty();
                    if ($userTags->isEmpty()){
                        UserTags::create([
                            'user_id'=>$userId,
                            'tag_id'=>2,
                            'platform'=>$user['platform'],
                        ]);
                    }
                }
                break;
            case 3:
                //mq延时队列触发，下单5分钟后调起，订单orderCallBack里面调起
               //支付未阅读
                $pay_time = $order['pay_time']??0;
                //获取用户阅读记录
                $bookread = Bookread::BookreadQuery($userId)
                    ->where('user_id', '=',$userId)
                    ->where('create_time', '>',$pay_time)
                    ->findOrEmpty();
                dump("支付未阅读callback-查询阅读记录 用户ID-" . $userId . date("Y-m-d H:i:s"), $userId);
                if ($bookread->isEmpty()){
                    $userTags = UserTags::where(['user_id'=>$userId,'tag_id'=>3])->findOrEmpty();
                    if ($userTags->isEmpty()){
                        UserTags::create([
                            'user_id'=>$userId,
                            'tag_id'=>3,
                            'platform'=>$user['platform'],
                        ]);
                    }
                }
                break;
            default:
                // 如果表达式的值与任何case都不匹配，执行这里的代码
        }
        return true;
    }

}
